Ext.onReady(function(){ 

 Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var sm = new Ext.grid.CheckboxSelectionModel();
var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'流水号',dataIndex:'serialNo',hidden:true},
{header:'被担保人姓名',dataIndex:'lenderName',width:80},
{header:'被担保人证件类型',dataIndex:'lenderIdtype',width:60},
{header:'被担保人证件号码',dataIndex:'lenderIdno',width:120}, 
{header:'为他人贷款合同担保金额',dataIndex:'guaranteeSum',width:80},
{header:'被担保贷款实际本金余额',dataIndex:'guaranteeBalance',width:80},
{header:'更新时间',dataIndex:'updateDate',width:80},
]); 
var ds=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'customer/listIndOguarantee.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'serialNo'},
{name:'lenderName'}, 
{name:'lenderIdtype'},  
{name:'lenderIdno'},  
{name:'guaranteeSum'},  
{name:'guaranteeBalance'},  
{name:'updateDate'},
]) 
}); 
ds.load({params:{start:0,limit:10}}); 

var addIDTypeStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=CertType',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) ,
	autoLoad:true
});  

var countryStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showCountry.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
countryStore.load();

var provinceStore = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showProvince.do',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'provinceCode'}, 
{name:'provinceName'}
]) 
}); 
provinceStore.load();


var grid=new Ext.grid.GridPanel({ 
id:'myCustomers',
renderTo:"customer_list", 
stripeRows:true,//斑马线效果
title:"客户担保信息", 
height:500, 
cm:cm,
store: ds,
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 10,   
                store: ds, 
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
 {text:'新增',id: 'btnAdd'},
 '-',
 {text:'查看/编辑',handler:checkMyCustomer},
 '-',
 {text:'删除',handler:deleteCustomer}
 ]              
});

//新增客户表单
addForm = new Ext.form.FormPanel({ 
id: 'addCustomerForm', 
width: 430, 
height: 300, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 180, 
items: 
[ { xtype: 'fieldset', 
title: '担保信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[
 { layout : "form",
           items : [{
        	   xtype: 'textfield',
   			id: 'lenderName', 
   			name: 'lenderName', 
   			fieldLabel: '被担保人姓名', 
   			emptyText: '必填', 
   			blankText: '被担保人姓名不能为空', 
   			allowBlank: false,  
   			width: 200,
}]},

{ layout : "form",
           items : [{
        	   xtype: 'combo', 
        		 id : "lenderIdtype",
        		              store :addIDTypeStore,
        		              fieldLabel: '被担保人证件类型',
        		              valueField : 'code',
        		              displayField : 'name',
        		              typeAhead : true,
        		              width: 200,
        		              mode:'remote', 
        		              emptyText : '请选择被担保人证件类型...',
        		              editable: false,
        		              triggerAction : 'all',
        		              selectOnFocus : true,
        		              hiddenName:'lenderIdtype'
}]},
 { layout : "form",
           items : [{
        	   xtype: 'textfield', 
        		id:'lenderIdno',
        		name: 'lenderIdno', 
        		fieldLabel: '被担保人证件号码', 
        		width: 200
}]},

{ layout : "form",
    items : [{
    	xtype: 'textfield',
    	id: 'guaranteeSum', 
    	name: 'guaranteeSum', 
    	fieldLabel: '为他人贷款合同担保金额（元）',
    	regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
		regexText:'精确到小数点后两位！' , 
    	allowBlank: true,  
    	allowDecimals:true,               
    	allowNegative:false, 
    	width: 200,
}]},

{ layout : "form",
           items : [{
xtype: 'textfield',
id: 'guaranteeBalance', 
name: 'guaranteeBalance', 
fieldLabel: '被担保贷款实际本金余额（元）',  
regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
regexText:'精确到小数点后两位！' , 
allowBlank: true,  
width: 200,
}]},

{ layout : "form",
    items : [{
xtype: 'hidden', 
id : "serialNo",
       fieldLabel: '担保ID',
       allowBlank: true
}]}

]}]});

//新增用户窗口 
addWin = new Ext.Window({ 
id: 'addCustomerWin', 
title: '客户基本信息-担保信息', 
width: 450, 
height: 300, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '保存', id: 'btnSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addCustomerWin').hide(); } }

] 
});

function addRoleFunction() {

	
	var addForm = Ext.getCmp("addCustomerForm");
	if(addForm.form.isValid()){

	 addForm.form.doAction('submit', { 

                    url : 'customer/addIndOguarantee.do', 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       
                       Ext.getCmp('myCustomers').store.reload();
                       Ext.Msg.alert('提示', '保存成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '保存失败！'); 
 
                        } 
                    });  
                    }
 }
  


//添加按钮单击事件 
function btnAddClick() { 
Ext.getCmp("addCustomerWin").show();
addForm.getForm().getEl().dom.reset(); 
Ext.getCmp("addCustomerWin").hide(); 

Ext.getCmp("addCustomerWin").buttons[0].handler = addRoleFunction; 
Ext.getCmp("addCustomerWin").show(); 
}; 

//新增按钮 
var addUserBtn = Ext.getCmp('btnAdd'); 
addUserBtn.on('click', btnAddClick); 

//删除地址
function deleteCustomer(){
var grid = Ext.getCmp('myCustomers'); 
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要删除的项！"); 
} 
else{
Ext.MessageBox.confirm('提示', '确实要删除所选项吗？', deleteConfirm);
}
}

function deleteConfirm(btn){
if (btn == 'yes') {
alert(grid.getSelectionModel().getSelections()[0].data.serialNo);
 Ext.Ajax.request({
  url:'customer/deleteIndOguarantee.do',
  params : { serialNo: grid.getSelectionModel().getSelections()[0].data.serialNo},
  method:'POST',
  success:function(){
   Ext.MessageBox.alert("提示","删除成功！");
   Ext.getCmp('myCustomers').store.reload(); 
  },failure:function(){
    Ext.MessageBox.alert("提示", "删除失败！"); 
  }
});
}
}

//编辑地址
function editAddressFunction(){
	
	var addForm = Ext.getCmp("addCustomerForm");

if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 

                    url : 'customer/editIndOguarantee.do', 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       
                       Ext.getCmp('myCustomers').store.reload();
                       Ext.Msg.alert('提示', '编辑地址成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '编辑地址失败！'); 
 
                        } 
                    }); 
                    }
}

function checkMyCustomer(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要编辑的地址！"); 
}
else{
	var serialNo = grid.getSelectionModel().getSelections()[0].data.serialNo;
	
	Ext.getCmp('addCustomerForm').form.reset(); 
	Ext.getCmp("addCustomerWin").buttons[0].handler = editAddressFunction; 

	Ext.Ajax.request({
	url:'customer/getIndOguarantee.do',
	params : { serialNo: grid.getSelectionModel().getSelections()[0].data.serialNo},
	method:'POST',
	success:function(response){
		//alert(response.responseText);
		var addJSON = Ext.util.JSON.decode(response.responseText);
		
		Ext.getCmp("serialNo").setValue(addJSON.serialNo);
		Ext.getCmp("lenderName").setValue(addJSON.lenderName);
	  	Ext.getCmp("lenderIdtype").setValue(addJSON.lenderIdtype);
		Ext.getCmp("lenderIdno").setValue(addJSON.lenderIdno);
	   	Ext.getCmp("guaranteeSum").setValue(addJSON.guaranteeSum);
		Ext.getCmp("guaranteeBalance").setValue(addJSON.guaranteeBalance);
		Ext.getCmp("addCustomerWin").show(); 
	
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 

/*	var addressStore = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'customer/getAddress.do?serialNo='+serialNo,
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'addressType'}, 
{name:'countryCode'},  
{name:'provinceCode'},  
{name:'cityCode'},  
{name:'addressDesc'},  
{name:'postCode'},  
{name:'tel'},  
{name:'fax'},  
{name:'webSiteUrl'}
]) 
}); 
addressStore.load();

var record = addressStore.data.items[0];
alert(record);
Ext.getCmp("addressDesc").setValue(record.addressDesc);
 */

}
}

function rowdblclickFn(grid, rowIndex, e){//双击事件  
             var row = grid.getStore().getAt(rowIndex); 
             var selectedCustId = row.get("custId");
             Ext.Ajax.request({
  url:'checkRequest.do',
  method:'GET',
  success:function(){
  window.location.href="checkMyCustomer.do?CustId="+selectedCustId;
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
});  
           //  window.location.href="view/customer/CheckCustomer_General.jsp?custId="+selectedCustId;
        }   
        grid.addListener('rowdblclick',checkMyCustomer);
/*grid.on('rowdblclick',function(){ window.location.href="view/customer/CheckCustomer_General.jsp" });*/
}); 